//
//  MainTitleScrollV.m
//  ZITHER
//
//  Created by weixin on 2018/8/27.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainTitleScrollV.h"

@implementation MainTitleScrollV

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _arrMNormal = [NSMutableArray array];
        self.bounces = NO;
        self.showsHorizontalScrollIndicator = NO;
//        self.SliderView.hidden = YES;
        self.backgroundColor = [UIColor orangeColor];
        
        [self initData];
    }
    return self;
}

-(void)initUI{
    
    [self addSubview:self.viewBlcak];
    UIButton *btn = [self viewWithTag:100];
    
    CGSize titleSize = [@"热搜" boundingRectWithSize:CGSizeMake(10000,15) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.SelectFont} context:nil].size;

    [self.viewBlcak mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btn.mas_centerX).offset(0);
        make.bottom.equalTo(btn.mas_bottom).offset(0);
        make.height.mas_equalTo(3);
        make.width.mas_equalTo(titleSize.width);
    }];
}


-(void)upViewBlcak:(NSInteger)index{
    NSString *str = self.arrMSelect[index-100];
    
    CGSize titleSize = [str boundingRectWithSize:CGSizeMake(10000,15) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.SelectFont} context:nil].size;
    
    [self.viewBlcak mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_left).offset(kScreenW/self.arrMNormal.count/2.0*((index-100)*2+1));
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.height.mas_equalTo(3);
        make.width.mas_equalTo(titleSize.width);
    }];
    
 
    
}

-(void)initData{
    self.NormalColor = [UIColor colorWithHex:@"999999"];
    self.NormalFont = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];
    self.SelectColor = [UIColor colorWithHex:@"333333"];
    self.SelectFont =  [UIFont fontWithName:@"PingFangSC-Semibold" size:14];
}

-(void)setArrMNormal:(NSMutableArray *)arrMNormal
{
    
    _arrMNormal = arrMNormal;
    
    self.contentOffset = CGPointMake(0, 0);
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIButton *lastBtn = nil;
    
    for (int i = 0; i<arrMNormal.count; i++) {
        
        UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor randomColor];
        button.tag = 100+i;
        
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        
        [button setTitle:self.arrMSelect[i] forState:UIControlStateNormal];
        
        [button setTitle:self.arrMSelect[i] forState:UIControlStateDisabled];

        [button setTitleColor:HexColor(0x999999) forState:UIControlStateNormal];
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
        
        CGFloat width = kScreenW/arrMNormal.count;
        
        button.frame = CGRectMake(width*i,0, width , 40);
        
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        
        @weakify(self);
        [[button rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
//            [self changeBtn:x.tag];
            if (self.block) {
                self.block(x.tag);
            }
        }];
        
//        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        if (i == 0) {
            lastBtn = button;
        }
    }
    self.contentSize = CGSizeMake(CGRectGetMaxX(lastBtn.frame), 40);
    [self initUI];
    [self changeBtn:100];

//    [self addSubview:self.SliderView];
}

-(void)btnClick:(UIButton*)btn
{
    [self changeBtn:btn.tag];
    if (_block) {
        self.block(btn.tag);
    }
}

-(void)changeBtn:(NSInteger)index{
    
    UIButton *btn = [self viewWithTag:index];
    self.lastButton.enabled = YES;
    btn.enabled = NO;
    
    [self upViewBlcak:index];

    // 告诉self.view约束需要更新
    [self setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束，若需要则更新，下面添加动画效果才起作用
    [self updateConstraintsIfNeeded];
    @weakify(self);

    [UIView animateWithDuration:0.3 animations:^{
        @strongify(self);

        [self layoutIfNeeded];
        
        [btn setTitleColor:self.SelectColor forState:UIControlStateNormal];
        btn.titleLabel.font = self.SelectFont;
        [self.lastButton setTitleColor:self.NormalColor forState:UIControlStateNormal];
        self.lastButton.titleLabel.font = self.NormalFont;
        
        CGRect frame = btn.frame;
        frame.origin.x=frame.origin.x+7.5;
        frame.origin.y=32;
        frame.size.height=2;
        frame.size.width = [NSString textWidth:btn.titleLabel.text font:16 height:16];
    }];
    
    //设置图标超出屏幕滚动到中间
    CGRect rect = btn.frame;
    rect.size.width += self.frame.size.width-30;
    rect.origin.x -= self.frame.size.width/2.0+20;
    [self scrollRectToVisible:rect animated:YES];
    
    self.lastButton = btn;
    
}

-(CGFloat)widthBtnHeight:(CGFloat)height font:(UIFont*)font text:(NSString*)str
{
    return  [str boundingRectWithSize:CGSizeMake(10000,height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size.width;
}

-(BaseView *)viewBlcak{
    if (!_viewBlcak) {
        _viewBlcak = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, kScreenW/3.0, 3)];
        _viewBlcak.backgroundColor = [UIColor blackColor];
    }
    return _viewBlcak;
}

@end
